var db = require('../../db/db');
var publictool = require("../../publictool/publictool");
var moment = require('moment');

exports.login = function (req, res) {
    var requestData = '';
    req.addListener('data', function (postDataChunk) {
        requestData += postDataChunk;
    });

    req.addListener('end', function () {
        publictool.putlog(req, requestData);
        var phone = publictool.checkoutdata(res, requestData);
        if (!phone) {
            return;
        }

        var client = db.client();

        var sql = 'SELECT lever,password,accesstoken,end_timestamp,isuse FROM user where account=?';
        var addSqlParams = [phone.account];
        client.query(sql, addSqlParams, function (err, result) {
            if (err) {
                console.log('[SELECT ERROR] - ', err.message);
                publictool.returnErr(res, '查询参数错误', 1);
                client.end();
                return;
            }

            if (result.length == 0) {
                publictool.returnErr(res, '无此用户', 6);
                client.end();
                return;
            }

            var resultdata = result[0];

            if(resultdata.isuse == '0'){
                publictool.returnErr(res, '账号已经被停用', 2);
                client.end();
                return;
            }

            if (phone.password != resultdata.password) {
                publictool.returnErr(res, '密码错误', 6);
                client.end();
                return;
            }

            var momentDate = moment(resultdata.end_timestamp);
            var enddata = momentDate.toDate();
            var now = new Date();
            if (enddata.getTime() < now.getTime()) {
                publictool.returnErr(res, '用户已经过期', 1);
                client.end();
                return;
            }

            console.log(req.headers["user-agent"].toLowerCase());
            if (!isphone(req.headers["user-agent"].toLowerCase())) {
                var value = {
                    data: {
                        accesstoken: resultdata.accesstoken,
                        lever: resultdata.lever,
                        account: phone.account
                    }
                };
                publictool.returnValue(res, value, '成功');
                client.end();
                return;
            }

            sql = 'SELECT * FROM device where sn=?';
            addSqlParams = [phone.sn];
            client.query(sql, addSqlParams, function (err, deviceresult) {
                if (err) {
                    console.log('[SELECT ERROR] - ', err.message);
                    publictool.returnErr(res, '查询参数错误', 1);
                    client.end();
                    return;
                }

                if (deviceresult.length == 0) {
                    publictool.returnErr(res, '没有找到此设备', 1);
                    client.end();
                    return;
                }

                var value = {
                    data: {
                        accesstoken: resultdata.accesstoken,
                        lever: resultdata.lever,
                        account: phone.account
                    }
                };
                publictool.returnValue(res, value, '成功');
                client.end();
                return;
            });
        });
    });
};

function isphone(deviceAgent) {
    return deviceAgent.match(/(iphone|ipod|ipad|android|darwin)/);
}